/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "los_event.h"
#include "los_task.h"
#include "los_api_event.h"
#include "los_inspect_entry.h"
#include "csg_sdk_hal.h"

#ifdef LOSCFG_LIB_LIBC
#include "string.h"
#endif

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

extern UINT32 uwTask1, uwTask2, uwTask3;
/* event control struct */
extern EVENT_CB_S  example_event;


/* wait event type */
#define event_wait 0x00000003

static VOID send_Event(VOID)
{
    UINT32 uwEvent;
    UINT32 uwRet = LOS_OK;

	static UINT32 TempData=0;
	
	while(TempData!=0xAA)
	{
		TempData=0;
		TempData=(USART0->RXDATA);
		if(TempData& USART_RXDATA_EMPTY)
		{
			TempData=0;
		}
		else
		{
			TempData=(TempData & 0xFF);
		}
		dprintf("\r\n7. task1 RUN. send '0xAA' to LOS_EventWrite\r\n");
		delay_1ms(1000);
	}

    uwEvent = 0x03;
    /* write event */
    dprintf("4. task1 write expect EventMask 0x%x .\r\n",uwEvent);
    uwRet = LOS_EventWrite(&example_event, uwEvent);
    if(uwRet != LOS_OK)
    {
        dprintf("\r\nevent write failed .\r\n");
        return LOS_NOK;
    }
}

static VOID recv_Event(VOID)
{
    UINT32 uwEvent;
    UINT32 uwRet = LOS_OK;
    UINT32 i=0;
    while(i<1)
    {
	    /*
	     * timeout, WAITMODE to read event, timeout is 100 ticks,
	     * if timeout, wake task directly
	     */
	    dprintf("\r\n3. task2 read event,wait EventMask 0x%x,mode is LOS_WAITMODE_AND| LOS_WAITMODE_CLR. \r\n",event_wait);
        dprintf("\r\ntask2 LOS_WAIT_FOREVER. \r\n");
	    uwEvent = LOS_EventRead(&example_event, event_wait, LOS_WAITMODE_AND| LOS_WAITMODE_CLR, LOS_WAIT_FOREVER);
	    
        if(uwEvent)
	    {
	        dprintf("5. Task2 read event success,read EventMask :0x%x . \r\n",uwEvent);
	    }
	    else
	    {
	        dprintf("\r\ntest err. \r\n");
	    }
        i++;
    }
    dprintf("5. Event management forever recv test success! \r\n");
}

UINT32 Example_Event_forever_recv(VOID)
{
    UINT32 uwRet;
    TSK_INIT_PARAM_S stInitParam1;
	
    dprintf("\r\n--------4.10 Event management event forever recv start!--------\r\n");
	
    LOS_TaskLock();//lock task schedue
    LOS_TaskDelete(uwTask1);
	LOS_TaskDelete(uwTask2);
    LOS_TaskDelete(uwTask3);
	
    /* event init */
    uwRet = LOS_EventInit(&example_event);
    if(uwRet != LOS_OK)
    {
        dprintf("init event failed .\r\n");
        return LOS_NOK;
    }
    dprintf("\r\n1. Initialize event control block success!\r\n");

    /* create task1 */
    stInitParam1.pfnTaskEntry = send_Event;
    stInitParam1.usTaskPrio = 2;
    stInitParam1.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    stInitParam1.pcName = "sendevent";	   	
    
    uwRet = LOS_TaskCreate(&uwTask1, &stInitParam1);
    if (uwRet != LOS_OK)
    {
        dprintf("create task1 failed!,error:%x \r\n",uwRet);
        return uwRet;
    }
    dprintf("\r\n1. Create task1 to send Evnet!\r\n");
	
    /*create task3*/	
    stInitParam1.usTaskPrio = 1;
    stInitParam1.pfnTaskEntry = recv_Event;	
    stInitParam1.pcName = "recvevent";
    uwRet = LOS_TaskCreate(&uwTask2, &stInitParam1);
    if (uwRet != LOS_OK)
    {
        dprintf("create task2 failed!,error:%x \r\n",uwRet);
        return uwRet;
    }
    dprintf("\r\n2. Create task2 to receive Event!\r\n");
	
    LOS_TaskUnlock();//unlock task schedue

    return LOS_OK;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
